﻿Public Class frmMain

    Private m_Characters As List(Of clsCharacter)

    Private m_SaveFile As SaveFile
    Private Structure SaveFile
        Dim OriginalName As String
        Dim EditName As String
        Dim Content() As Byte
        Dim IsDirty As Boolean
    End Structure

    Private Sub frmMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If m_SaveFile.IsDirty Then
            Dim msgbtn As DialogResult = MessageBox.Show("Save changes?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

            Select Case msgbtn
                Case Windows.Forms.DialogResult.Yes
                    IO.File.Copy(m_SaveFile.EditName, m_SaveFile.OriginalName, True)
                Case Windows.Forms.DialogResult.Cancel
                    e.Cancel = True
            End Select
        End If
    End Sub

    Private Sub miOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miOpenFile.Click, tsiOpenFile.Click

        If m_SaveFile.IsDirty Then
            ' Attempt to Save file
        End If

        Using ofd As New OpenFileDialog
            With ofd
                .Title = "Eye of the Beholder I"
                .AutoUpgradeEnabled = True
                .Filter = "EOB Savegame (*.sav)|*.sav"

                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    m_Characters = New List(Of clsCharacter)
                    m_SaveFile = New SaveFile

                    m_SaveFile.OriginalName = .FileName

                    m_SaveFile.EditName = IO.Path.GetTempFileName

                    IO.File.Copy(.FileName, m_SaveFile.EditName, True)

                    Using f As New IO.FileStream(m_SaveFile.EditName, IO.FileMode.Open, IO.FileAccess.Read)
                        m_SaveFile.Content = New Byte((f.Length) - 1) {}
                        Call f.Read(m_SaveFile.Content, 0, f.Length)
                        f.Close()
                    End Using

                    For i As Integer = 0 To 3
                        m_Characters.Add(New clsCharacter(i + 1, i * 243))
                        If m_Characters(i).Load(m_SaveFile.Content) Then
                            Dim cn As TreeNode = New TreeNode(m_Characters(i).Name, 1, 1)
                            cn.Tag = i
                            Me.TreeView1.Nodes(0).Nodes.Add(cn)

                        End If
                    Next
                End If
            End With
        End Using
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        If e.Node.Parent Is Nothing Then Return
        Call m_Characters(e.Node.Tag).Show(Me)
    End Sub

    Private Sub btnRest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRest.Click

        Dim oChar As clsCharacter = m_Characters(Me.TreeView1.SelectedNode.Tag)

        m_SaveFile.Content(oChar.StartOffset + 27) = oChar.HPMax

        Using f As New IO.FileStream(m_SaveFile.EditName, IO.FileMode.Open, IO.FileAccess.Write)
            Call f.Write(m_SaveFile.Content, 0, m_SaveFile.Content.Length)
            f.Close()
        End Using

        oChar.Load(m_SaveFile.Content)
        oChar.Show(Me)

        m_SaveFile.IsDirty = True

    End Sub

    Private Sub btnEat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEat.Click
        Dim ochar As clsCharacter = m_Characters(Me.TreeView1.SelectedNode.Tag)

        m_SaveFile.Content(ochar.StartOffset + 35) = 100

        Using f As New IO.FileStream(m_SaveFile.EditName, IO.FileMode.Open, IO.FileAccess.Write)
            Call f.Write(m_SaveFile.Content, 0, m_SaveFile.Content.Length)
            f.Close()
        End Using

        ochar.Load(m_SaveFile.Content)
        ochar.Show(Me)

        m_SaveFile.IsDirty = True
    End Sub

    Private Sub tsiRestParty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsiRestParty.Click

        Dim oChar As clsCharacter = Nothing

        For i As Integer = 0 To m_Characters.Count - 1
            oChar = m_Characters(i)
            m_SaveFile.Content(oChar.StartOffset + 27) = oChar.HPMax
        Next

        Using f As New IO.FileStream(m_SaveFile.EditName, IO.FileMode.Open, IO.FileAccess.Write)
            Call f.Write(m_SaveFile.Content, 0, m_SaveFile.Content.Length)
            f.Close()
        End Using

        For i As Integer = 0 To m_Characters.Count - 1
            m_Characters(i).Load(m_SaveFile.Content)
        Next

        m_Characters(Me.TreeView1.SelectedNode.Tag).Show(Me)
        
        m_SaveFile.IsDirty = True

        MessageBox.Show("The party is rested.")
    End Sub

    Private Sub tsiFeedParty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsiFeedParty.Click
        Dim oChar As clsCharacter = Nothing

        For i As Integer = 0 To m_Characters.Count - 1
            oChar = m_Characters(i)
            m_SaveFile.Content(oChar.StartOffset + 35) = 100
        Next

        Using f As New IO.FileStream(m_SaveFile.EditName, IO.FileMode.Open, IO.FileAccess.Write)
            Call f.Write(m_SaveFile.Content, 0, m_SaveFile.Content.Length)
            f.Close()
        End Using

        For i As Integer = 0 To m_Characters.Count - 1
            m_Characters(i).Load(m_SaveFile.Content)
        Next

        m_Characters(Me.TreeView1.SelectedNode.Tag).Show(Me)

        m_SaveFile.IsDirty = True

        MessageBox.Show("The party has been fed.")

    End Sub

    Private Sub miSaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miSaveFile.Click, tsiSaveFile.Click
        Using f As New IO.FileStream(m_SaveFile.EditName, IO.FileMode.Open, IO.FileAccess.Write)
            Call f.Write(m_SaveFile.Content, 0, m_SaveFile.Content.Length)
            f.Close()
        End Using

        m_SaveFile.IsDirty = False

    End Sub
End Class
